#ifndef MEMORYPOOL_H_61_
#define MEMORYPOOL_H_61_

//! 一个内存页大小
#define PAGELEN 8
//! 一个内存池单元的内存页长度
#define POOLUNITLEN 1024

//! 获取len长度所占内存空间大小
#define ROUND_UP(len) ((len + PAGELEN - 1) & ~(PAGELEN - 1))

//! 获取所申请内存在内存链列表中的下标
#define CHUNKINDEX(len) ((len  - 1) / PAGELEN)

typedef union memorychunk_u MemoryChunk;
typedef struct memorypoolunit_s MemoryPoolUnit;

union memorychunk_u
{
	MemoryChunk* next_free_chunk;
	char data[1];
};

struct memorypoolunit_s
{
	MemoryPoolUnit* prev;
	MemoryPoolUnit* next;
	int cur;
	int len;
};

class CMemoryPool
{
public:
	CMemoryPool();
	~CMemoryPool();

	void* alloc_from_chunk(unsigned int len);
	void dealloc_to_chunk(void* buf, unsigned int len);

private:
	int refillMemoryChunk(MemoryChunk** ppChunk, unsigned int len);

private:
	MemoryPoolUnit* m_pMemoryUnit;
	MemoryChunk* m_pMemoryChunks[16];
};

#endif